package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.base.util.DateUtil;
import com.coding.dao.CollectDao;
import com.coding.dao.CommentDao;
import com.coding.dao.PostDao;
import com.coding.dao.ThumbDao;
import com.coding.pojo.Collect;
import com.coding.pojo.Post;
import com.coding.pojo.Thumb;
import com.coding.service.ThumbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @auther wangchenghao
 * @date 2020/9/22
 **/
@Service
public class ThumbImpl implements ThumbService {
    @Autowired
    ThumbDao thumbDao;
    @Autowired
    CollectDao collectDao;
    @Autowired
    CommentDao commentDao;
    @Autowired
    PostDao postDao;

    @Override
    public void addThumb(int user_id, int source_id, String source_kind, BaseModel baseModel) throws Exception {
        Thumb lastThumb=thumbDao.selectAll(user_id,source_id,source_kind);
        if (lastThumb!=null){
            baseModel.setFailure("已添加点赞");
            return;
        }
        Thumb thumb = new Thumb();
        thumb.setThumb_user_id(user_id);
        thumb.setThumn_source_id(source_id);
        thumb.setThumb_kind(source_kind);
        thumb.setThumb_time(DateUtil.getNow());
        if (thumbDao.insertSelective(thumb) == 1) {
            Post newPost=new Post();
            newPost=postDao.selectByPrimaryKey(source_id);
            int collectNumbers = collectDao.countCollectTimes(source_id, "post");
            if (collectNumbers > 0) {
                newPost.setPost_collect(collectNumbers);
            } else {
                newPost.setPost_collect(0);
            }
            int thumbNumbers = thumbDao.countThumb(source_id, "post");
            if (thumbNumbers > 0) {
                newPost.setPost_likes(thumbNumbers);
            } else {
                newPost.setPost_likes(0);
            }
            int commentNumbers = commentDao.countComments(source_id, "post");
            if (commentNumbers > 0) {
                newPost.setPost_comments(commentNumbers);
            } else {
                newPost.setPost_comments(0);
            }
            Collect thecollect = collectDao.selectAll(user_id, newPost.getPost_id(), "post");
            if (thecollect != null) {
                newPost.setPost_collect_status(1);
            }
            Thumb thethumb = thumbDao.selectAll(user_id, newPost.getPost_id(), "post");
            if (thethumb != null) {
                newPost.setPost_likes_status(1);
            }
            baseModel.setSucceed("成功新增点赞", newPost);
        } else {
            baseModel.setFailure("新增点赞失败");
        }
    }

    @Override
    public void deleteThumb(int user_id, int source_id, String source_kind, BaseModel baseModel) throws Exception {
        if (thumbDao.deleteThumb(user_id, source_id, source_kind) == 1) {
            Post newPost=new Post();
            newPost=postDao.selectByPrimaryKey(source_id);
            int collectNumbers = collectDao.countCollectTimes(source_id, "post");
            if (collectNumbers > 0) {
                newPost.setPost_collect(collectNumbers);
            } else {
                newPost.setPost_collect(0);
            }
            int thumbNumbers = thumbDao.countThumb(source_id, "post");
            if (thumbNumbers > 0) {
                newPost.setPost_likes(thumbNumbers);
            } else {
                newPost.setPost_likes(0);
            }
            int commentNumbers = commentDao.countComments(source_id, "post");
            if (commentNumbers > 0) {
                newPost.setPost_comments(commentNumbers);
            } else {
                newPost.setPost_comments(0);
            }
            Collect thecollect = collectDao.selectAll(user_id, newPost.getPost_id(), "post");
            if (thecollect != null) {
                newPost.setPost_collect_status(1);
            }
            Thumb thumb = thumbDao.selectAll(user_id, newPost.getPost_id(), "post");
            if (thumb != null) {
                newPost.setPost_likes_status(1);
            }
            baseModel.setSucceed("删除点赞成功",newPost);
        } else {
            baseModel.setFailure("删除点赞失败");
        }
    }

    @Override
    public void checkPost(int user_id, int source_id, String source_kind, BaseModel baseModel) throws Exception {
        Thumb lastThumb=thumbDao.selectAll(user_id,source_id,source_kind);
        if (lastThumb!=null){
            baseModel.setSucceed("已添加点赞");
        }else {
            baseModel.setFailure("帖子尚未点赞");
        }
    }
}
